﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Utilities;

namespace QuanLyKhachSan
{
    public partial class frmCauHinh : XtraForm
    {
        public frmCauHinh()
        {
            InitializeComponent();
        }

        private void cbbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool check = cbbAuthentication.SelectedIndex != 0;
            txtUsername.Enabled = txtPassword.Enabled = check;
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            string strConnection;
            if (cbbAuthentication.SelectedIndex != 0)
            {
                strConnection = String.Format("server={0};Database={1};User Id={2};Password={3};",
                    txtServername.EditValue,
                "master",
                txtUsername.EditValue,
                txtPassword.EditValue)
                ;
            }
            else
            {
                strConnection = String.Format("server={0};Database={1};Integrated security=true;",
                    txtServername.EditValue,
                    "master");
            }

            try
            {
                var con = new SqlConnection(strConnection);
                var cmd = new SqlCommand("SP_DATABASES", con);
                SqlDataReader reader = null;
                try{
                    con.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        cbbDatabase.Properties.Items.Add(reader[0].ToString());
                    }
                    cbbDatabase.Enabled = true;
                    XtraMessageBox.Show("Kết nối thành công.....", "thông báo", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
                catch
                {
                    XtraMessageBox.Show("Lỗi kế nối, vui lòng thử lại", "Lỗi");
                }
                finally
                {
                    if (con.State == ConnectionState.Open)
                        con.Close();
                    con.Dispose();
                    cmd.Dispose();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                string server, database;string username = "";
                string password = "";
                server = txtServername.EditValue.ToString();
                database = cbbDatabase.SelectedItem.ToString();

                if (cbbAuthentication.SelectedIndex != 0)
                {
                    password = txtPassword.EditValue.ToString();
                    username = txtUsername.EditValue.ToString();
                }
                ConfigHelper.WriteConfigToFile(server, database, username, password);

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
            finally
            {
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}